package com.app.scale.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.scale.basis.Constants;
import com.app.scale.bean.LoginBean;
import com.app.scale.bean.MenuBean;
import com.app.scale.bean.UserInfoBean;
import com.app.scale.login.LoginActivity;
import com.app.scale.R;
import com.app.scale.home.HomeBaseActivity;
import com.app.scale.other.PdfScanActivity;
import com.app.scale.set.AboutUsActivity;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.ActivityTool;
import my.MySharedPreferences;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;


public class UserIndexActivity extends HomeBaseActivity {


    @BindView(R.id.recyclerview)
    RecyclerView listView;
    
    @BindView(R.id.tv_name)
    TextView textName;
    @BindView(R.id.tv_content)
    TextView textContent;
    @BindView(R.id.img_photo)
    ImageView img_photo;
    MyAdapter mAdapter;
    List<MenuBean> mListBean;


    @Override
    public void initViews() {
        setContentView(R.layout.user_index_activity);
        super.initViews();
        setTitleBg(R.color.transparent);
        ((TextView) findViewById(R.id.base_title_text)).setTextColor(getResources().getColor(R.color.white));
        setTitle("我的");
        setTitleRightButton(R.drawable.icon_set, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logout();
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        MySharedPreferences mSp = new MySharedPreferences(this);
        textName.setText("");
        textContent.setText("" + mSp.getUser());
        listView.setLayoutManager(new LinearLayoutManager(mContext));
    }


    @Override
    protected void onResume() {
        super.onResume();
        showUserInfo();
//        getImmersionBar().statusBarDarkFont(true,0.5f);
//        getImmersionBar().statusBarDarkFont(true).init();
    }
    void showUserInfo(){
        if(LoginBean.getInstance().getRight()!= null){
            mListBean = LoginBean.getInstance().getParentMenuList();
            if(mAdapter == null){
                mAdapter = new MyAdapter();
                listView.setAdapter(mAdapter);
            }else {
                mAdapter.notifyDataSetChanged();
            }

        }
    }

    @Override
    public void onDoubleClick() {
        super.onDoubleClick();
        if(isFinishing())return;
        getUserInfo();
    }
    void getUserInfo() {
        MyParams params = new MyParams();
//        showProgress();
        params.put("appType","APP");
//        params.put(MyParams.PARAMS_LOG,"false");
        MyHttpUtils.post(Constants.URL_USER_REFRESH_LOGININFO, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        UserInfoBean bean = (UserInfoBean) result;
                        LoginBean loginBean = LoginBean.getInstance();
                        if(loginBean == null)return;
                        loginBean.save();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
//                        super.onFailure(httpWhat, result);
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        if (isFinishing()) return;
                        hideLoading();
                    }
                },
                0, UserInfoBean.class);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void setUserView() {
//        textName.setText(userInfoDetailBean.getName());
//        textContent.setText("职位：" + userInfoDetailBean.getPost());
    }



    void logout() {
        new QMUIDialog.MessageDialogBuilder(mContext).setMessage("退出当前账号？").addAction("取消", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                dialog.dismiss();
            }
        }).addAction(0, "立即退出", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                LoginActivity.toLoginAllClear(mContext);
            }
        }).create().show();
    }


//    void modifypwd() {
//        ActivityTool.skipActivity(mContext, ModifyPwdActivity.class);
//    }
    

    private static final int REQUEST_QRCODE = 11;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }



    void help() {
        PdfScanActivity.toPdfScanActivity(mContext, "智能秤称重部分使用使用说明.pdf", "用户手册");
    }

    void aboutUs() {
        ActivityTool.skipActivity(mContext, AboutUsActivity.class);
    }

    
       public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

        public MyAdapter() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.base_list_item_and_right_arrow, viewGroup, false);
            return new MyAdapter.ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.img_icon)
            public ImageView img_icon;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                MenuBean data = mListBean.get(position);
                text_name.setText(data.getMna());
                img_icon.setVisibility(View.GONE);
                list_item.setTag(position);
            }

            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
//                int position = (int) v.getTag();
//                ActivityTool.skipActivityForResult(mContext, HomeAppActivity.class, 1);
            }
        }
    }

    
}
